package com.lilanz.microservice.express.controller;

import com.lilanz.microservice.express.entity.Certificate;
import com.lilanz.microservice.express.entity.PrintBean;
import com.lilanz.microservice.express.entity.ShunFengExpress;
import com.lilanz.microservice.express.service.serviceimpl.CertificateServiceImpl;
import com.lilanz.microservice.express.utils.ShunFengService;
import com.lilanz.microservice.express.utils.exception.MyException;
import com.lilanz.microservice.express.utils.util.JsonData;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@RestController
@Api("顺丰快递操作")
public class Controller {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CertificateServiceImpl certificateService;

    @ApiOperation(value = "下单",
            notes = "此顺丰下单是寄中国大陆内的快递,json最外层200表示成功连接上顺丰公司500是还未连接顺丰公司就已经发生错误," +
                    "在返回200基础上 每个json对象的200和500分别代表下单成功和下单失败")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功连接上顺丰公司"),
            @ApiResponse(code = 500, message = "发生错误一般是你提交报文格式不匹配"),
    })
    @PostMapping("/commit_order")
    public Object commitOrder(@RequestBody List<ShunFengExpress> expresses) {
        logger.info(expresses.toString());
        Certificate certificate = certificateService.selectCertificate();
        ShunFengService shunFengService = new ShunFengService(certificate);
        return shunFengService.sfExpressCommit(expresses);
    }


    @ApiOperation(value = "取消订单",
            notes = "json最外层200表示成功连接上顺丰公司500是还未连接顺丰公司就已经发生错误," +
                    "在返回200基础上 每个json对象的200和500分别代表取消订单成功和取消订单失败")
    @ApiImplicitParam(name = "orderId", value = "订单号", required = true)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功连接上顺丰公司"),
            @ApiResponse(code = 500, message = "发生错误一般是你提交报文格式不匹配"),
    })
    @DeleteMapping("/cancel_order/{orderId}")
    public Object cancelOrder(@PathVariable("orderId") String orderId) {
        logger.info("订单号:" + orderId);
        Certificate certificate = certificateService.selectCertificate();
        ShunFengService shunFengService = new ShunFengService(certificate);
        return shunFengService.sfExpressCancel(orderId);
    }


    @ApiOperation(value = "看看订单",
            notes = "json最外层200表示成功连接上顺丰公司500是还未连接顺丰公司就已经发生错误," +
                    "在返回200基础上 每个json对象的200和500分别代表查询成功和查询失败")
    @ApiImplicitParam(name = "orderId", value = "订单号", required = true)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功连接上顺丰公司"),
            @ApiResponse(code = 500, message = "发生错误一般是你提交报文格式不匹配"),
    })
    @GetMapping("/check_order")
    public Object checkOrder(@RequestParam("orderId") String orderId) {
        logger.info("订单号:" + orderId);
        Certificate certificate = certificateService.selectCertificate();
        ShunFengService shunFengService = new ShunFengService(certificate);
        return JsonData.buildSuccess(shunFengService.sfExpressCheck(orderId, "查询成功"));
    }


    @ApiOperation(value = "打印电子面单")
    @PostMapping("/print_order")
    public void printOrder(HttpServletResponse response, @RequestBody List<PrintBean> printBean) {
        logger.info(printBean.toString());
        Certificate certificate = certificateService.selectCertificate();
        ShunFengService shunFengService = new ShunFengService(certificate);
        shunFengService.sfExpressPrint(response, printBean);
    }


    @ApiOperation(value = "查询运单号的路线轨迹")
    @GetMapping("/route_message")
    public Object routeMessage(@RequestParam String mailno) {
        logger.info("运单号:" + mailno);
        Certificate certificate = certificateService.selectCertificate();
        ShunFengService shunFengService = new ShunFengService(certificate);
        return JsonData.buildSuccess(shunFengService.sfExpressRoute(mailno));
    }



}


